package com.appspot.thinkhea;

import java.util.Vector;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.appspot.thinkhea.model.MyDBHelper;
import com.appspot.thinkhea.model.Picture;
import com.appspot.thinkhea.model.Player;
import com.appspot.thinkhea.model.Record;
import com.appspot.thinkhea.view.GridRecordAdapter;

public class PanelGridPlayerRecord extends Activity {
	/** Called when the activity is first created. */
	private Vector<Picture> p;
	private Record r;
	private GridView recordGridView;
	private String tag = "PanelGridPicture";
	private int record_id = -1;
	private MyDBHelper db;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setBackgroundDrawableResource(R.drawable.bg);
		setTitle(R.string.desc_record_list);
		setContentView(R.layout.panel_grid_record);
		
		Bundle bundle = this.getIntent().getExtras();
		record_id = Integer.parseInt(bundle.getString("RECORD_ID"));

		// Add Button Listener
		Button b = (Button) findViewById(R.id.BtnGoBack);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent myIntent = new Intent(PanelGridPlayerRecord.this,
						PanelListRecord.class);
				PanelGridPlayerRecord.this.startActivity(myIntent);
			}
		});
		
		Button br = (Button) findViewById(R.id.BtnRemoveRecord);
		br.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				removeRecord();
			}
		});
		
		Button bs = (Button) findViewById(R.id.BtnSaveRecord);
		bs.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				saveRecord();
			}
		});

		// handle list event and model
		recordGridView = (GridView) findViewById(R.id.RecordGridView);
		recordGridView.setFocusable(true);
		recordGridView.setClickable(true);
		recordGridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> a, View v, int position,
					long id) {
				GridRecordAdapter ga = ((GridRecordAdapter)a.getAdapter());
				Picture c = ga.getItem(position);
				int pid = c.getId();
				Vector<Player> p = r.getPlayerList();
				for(int e =0;e<p.size();e++){
					Player q = p.get(e);
					int cid = q.getId();
					if(pid==cid){
						int k = q.getValue();
						q.setValue(++k);
					}
				}
				ga.notifyDataSetInvalidated();
			}			
		});
		recordGridView.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> a, View v, int position,
					long id) {
				GridRecordAdapter ga = ((GridRecordAdapter)a.getAdapter());
				Picture c = ga.getItem(position);
				int pid = c.getId();
				Vector<Player> p = r.getPlayerList();
				for(int e =0;e<p.size();e++){
					Player q = p.get(e);
					int cid = q.getId();
					if(pid==cid){
						int k = q.getValue();
						q.setValue(--k);
					}
				}
				ga.notifyDataSetInvalidated();
				return true;
			}			
		});
		loadRecord();
	}

	protected void saveRecord() {
		long result = -1;
		if(db==null){
			db= new MyDBHelper(PanelGridPlayerRecord.this);
		}
		db.open();
		result = db.updateRecord(r);
		db.close();
		String msg = "Record updated.";
		if(result==-1){
			msg = "Fail in updating record.";
		}
		GridRecordAdapter ga = ((GridRecordAdapter)recordGridView.getAdapter());
		ga.setInitRecord(r);
		ga.notifyDataSetInvalidated();
		ga.notifyDataSetChanged();
		Toast.makeText(PanelGridPlayerRecord.this, msg,
				Toast.LENGTH_SHORT).show();
	}

	protected void removeRecord() {	
		new AlertDialog.Builder(PanelGridPlayerRecord.this).setTitle(
				"Remove Record").setMessage(
				"Are you sure you want to remove this record?")
				.setPositiveButton("OK",
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialoginterface,
									int id) {
								long result = -1;
								if(db==null){
									db= new MyDBHelper(PanelGridPlayerRecord.this);
								}
								db.open();
								result = db.removeRecordByID(record_id);
								db.close();
								String msg = "Record removed.";
								if(result==-1){
									msg = "Cannot remove record.";
								}
								Toast.makeText(PanelGridPlayerRecord.this, msg,
										Toast.LENGTH_SHORT).show();
								Intent myIntent = new Intent(PanelGridPlayerRecord.this,
										PanelListRecord.class);
								PanelGridPlayerRecord.this.startActivity(myIntent);
							}
						}).setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialoginterface,
									int id) {
								dialoginterface.dismiss();
							}
						}).show();
	}

	public void loadRecord() {
		// load data from DB
		if (p != null) {
			p.clear();
		}else{
			p = new Vector<Picture>();
		}
		if(db==null){
			db= new MyDBHelper(this);
		}
		db.open();
		r = db.getRecordByID(record_id);
		Vector<Player> l = r.getPlayerList();
		for(int a=0;a<l.size();a++){
			int pid = l.get(a).getId();
			if(pid!=-1){
				Picture c = db.getPictureByID(pid);
				if(c!=null){
					p.add(c);				
				}
			}
		}
		db.close();
		recordGridView.setAdapter(new GridRecordAdapter(this, p,r));
	}
}